<?php
/**
* 新增到个人用户
* @author xulei
* @since v1.0
* @modify 2014.9.25
* @copyright 深圳亿游
*/
require_once(LIB_PATH.'controller/root.con.php');
class TempUpperCon extends rootCon {
	public function listAction() {
        $modelObj = new TempUpperExt();
        if(!r('sql')){
            $modelObj->load();
            $paramNames = $modelObj->loadedFiled();
            $where = $modelObj->getFindSql();

            if(array_key_exists('status',$modelObj->field())){
                if(r('status')==STATUS_ADMIN_DELETE){
                    ;//DO NOTHING
                }else if($where){
                    $where = '`status`!='.STATUS_ADMIN_DELETE.' AND '.$where;
                }else{
                    $where = '`status`!='.STATUS_ADMIN_DELETE.'  ';
                }
            }


            //---begin 外表查询---
            $foreignFields = $modelObj->foreignField();
            foreach($foreignFields as $fKey=>$fTable){
                $keyword = trim(addslashes(r($fKey.'Keyword')));
                if($keyword){//外表的关键词查询
                    $className = mymodel::getClassName($fTable);
                    if(!$className) continue;
                    $fObj = new $className();
                    $allF = $fObj->field();
                    $i = 0;
                    foreach($allF as $keywordF=> $f){
                        if($i ==0){//外表的主键
                            $primaryF = $keywordF;
                        }
                        if($i ==1){
                            break;
                        }
                        $i++;
                    }
                    $sql = "SELECT `$primaryF`,`$keywordF`  FROM ".$fObj->table()." WHERE `$keywordF` LIKE '%$keyword%'";
                    $all = $fObj->query($sql,"ORDER BY $primaryF DESC  LIMIT 10000");
                    $allIds = [-1];
                    foreach($all as $item){
                        $allIds[] = $item[$primaryF];
                    }

                    if(is_numeric($keyword) && $keyword>0){//支持主键查询
                        $allIds[] = $keyword;
                    }


                    $allIds = join(",",$allIds);
                    if($where){
                        $where = " `$fKey` IN ($allIds) ".' AND '.$where;
                    }else{
                        $where = " `$fKey` IN ($allIds) ";
                    }
                }
            }
            //---end 外表查询---

            if($where) $where = ' WHERE '.$where;
            $sql = 'SELECT * FROM  '.$modelObj->table().'  '.$where ;
            $orderBy = r('orderBy');
            if(!$orderBy){
                $orderBy = " ORDER BY ".$modelObj->key().' DESC';
            }
            if(!preg_match("/\s+order\s+/i",$sql)){
                $sql .= $orderBy;
            }
        }else{
            if(!SQL_ADMIN) die("404");

            $sql = trim($_GET['sql']);
            $reg = "/\;+$/i";
            $sql = preg_replace($reg,'',$sql);
        }

        $paramNames[] = 'model';
        $paramNames[] = 'sql';

        $pagerObj=new mypager($sql,$paramNames);
        $pagerObj->setPageSize(DEFAULT_PAGE_SIZE);
        $currentList = $pagerObj->getResult();

        $extFile = ROOT_PATH.'view/templower,list.ext.view.php';
        if(file_exists($extFile)){
            require_once($extFile);
        }else{
            require_once(ROOT_PATH."view/templower,list.view.php");
        }
    }

    //只有逻辑删除
    public function delAction(){
        $this->saveAction();
    }

    public function saveAction(){
        $modelObj = new TempUpperExt();
        $modelObj->load();
        $modelObj->save();
        require_once(LIB_PATH."view/admin,save.view.php");
    }


    public function saveJsonAction(){
        header('Content-type:text/json');
         $modelObj = new TempUpperExt();
        $modelObj->load();
        $find = $modelObj->existsKeyVal();
        if(!$find){
            $this->j(-1,'需要设置主键');
        }

        $rect = $modelObj->save();


        $reasonField = $modelObj->reasonField();
        foreach($reasonField as $f){
            if(!$f) continue;

            //保存
            $key = $modelObj->key();
            $tbl = $modelObj->table()."_reason";
            $className = mymodel::getClassName($tbl);
            $tmpObj = new $className();
            $tmpObj->reason = r('reason');
            $tmpObj->status = r('status');
            $tmpObj->$key = r($key);
            $tmpObj->save();
        }

        $this->j(0,'保存',$rect);
    }

    //@todo check if admin is login
    public function editAction(){
        $modelObj = new TempUpperExt();
        $modelObj->load();
        $find = $modelObj->find();

        $defaultModelObj = new TempUpperExt();//对比默认值

        $extFile = ROOT_PATH.'view/templower,edit.ext.view.php';
        if(file_exists($extFile)){
            require_once($extFile);
        }else{
            require_once(ROOT_PATH."view/templower,edit.view.php");
        }
    }

}

